package com.live.dengdengtv.activity.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.CustomTitleActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.net.NetTaskList;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.TIMFriendshipManager;
import com.tencent.TIMUserProfile;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyf on 2016/8/18.
 */
public class ContributionActivity extends CustomTitleActivity{

    private static final String TAG = "ContributionActivity";

    private ListView listView;
    private MyAdapter adapter;

    private long virTicket = 0;

    private ArrayList<ItemDataBean> dataListView = new ArrayList<ItemDataBean>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_contribution,R.layout.title_custom_red);
        setDefaultTitle("我的贡献");

        listView = (ListView) findViewById(R.id.listView);

        adapter = new MyAdapter();
        listView.setAdapter(adapter);

        getContributionList();

        //虚拟券
        TextView virtual_ticket_tv = (TextView) findViewById(R.id.virtual_ticket_tv);
        try {
            virTicket = Long.parseLong(LoginInfo.getInstance().getgUserInfo().virtual_ticket);
        }catch (Exception e){
            e.printStackTrace();
        }
        virtual_ticket_tv.setText(virTicket+"");

    }


    private void getContributionList(){
        requestData(NetTaskList.get_contribution_list, AppInfo.getHeadInfo(),1);
    }


    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            if(response.result == 0){
                JsonArray array = response.data.getAsJsonArray();
                for (int i=0; i<array.size() ;i++){
                    ItemDataBean liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), ItemDataBean.class);
                    dataListView.add(liveItem);
                }
                getUserListInfo();
            }else {
                Toast.makeText(context,"暂无贡献",Toast.LENGTH_SHORT).show();
            }
        }
        return false;
    }


    private void getUserListInfo(){

        List<String> users = new ArrayList<String>();
        for (ItemDataBean item : dataListView) {
            users.add(item.buyer_id);
        }

        if(users.size()<=0) return;

        TIMFriendshipManager.getInstance().getUsersProfile(users, new TIMValueCallBack<List<TIMUserProfile>>() {
            @Override
            public void onError(int code, String desc) {
                Log.e(TAG, "getUsersProfile failed: " + code + " desc" + desc);
            }
            @Override
            public void onSuccess(List<TIMUserProfile> timUserProfilesList) {
                for(TIMUserProfile item : timUserProfilesList){

                   for(int i=0;i<dataListView.size();i++){
                       if(dataListView.get(i).buyer_id.equals(item.getIdentifier())){
                           dataListView.get(i).nickName = item.getNickName();
                           dataListView.get(i).faceUrl = item.getFaceUrl();
                           dataListView.get(i).sex = item.getGender().getValue() +"";
                           break;
                       }
                   }

                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return dataListView.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String buyer_id = dataListView.get(position).buyer_id;
            String faceUrl = dataListView.get(position).faceUrl;
            String nickName = dataListView.get(position).nickName;
            String sex = dataListView.get(position).sex;
            String s_coin = dataListView.get(position).s_coin;

            RelativeLayout rl;
            if(position == 0){
                rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute1, null);
            }else if(position == 1){
                rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute2, null);
            }else if(position == 2){
                rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute3, null);
            }else {
                rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute, null);
                TextView msg_tv = (TextView) rl.findViewById(R.id.no_tv);
                msg_tv.setText("NO."+(position+1));
            }

            ImageView head_iv = (ImageView) rl.findViewById(R.id.head_iv);
            TextView name_tv = (TextView) rl.findViewById(R.id.name_tv);
            ImageView sex_iv = (ImageView) rl.findViewById(R.id.sex_iv);
            ImageView level_iv = (ImageView) rl.findViewById(R.id.level_iv);
            TextView contribute_tv = (TextView) rl.findViewById(R.id.contribute_tv);

            if(!TextUtils.isEmpty(faceUrl)){
                ImageLoader.getInstance().displayImage(faceUrl, head_iv);
            }

            if(!TextUtils.isEmpty(nickName)){
                name_tv.setText(nickName);
            }else {
                name_tv.setText(buyer_id);
            }

            if("1".equals(sex)){
                sex_iv.setImageResource(R.mipmap.my_iconsex_man);
            }
            contribute_tv.setText(s_coin);

            return rl;
        }
 }



    private static class ItemDataBean{
        public String buyer_id = "";//用户id
        public String faceUrl = "";
        public String nickName = "";
        public String sex = "";
        public String level = "";
        public String s_coin = "";//虚拟券
        public String no = "";
    }

    private View setListView() {

        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 10;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                RelativeLayout rl;
                if(position == 0){
                    rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute1, null);
                    TextView contribute_tv = (TextView) rl.findViewById(R.id.contribute_tv);
                    TextView name_tv = (TextView) rl.findViewById(R.id.name_tv);
                }else if(position == 1){
                    rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute2, null);
                    TextView contribute_tv = (TextView) rl.findViewById(R.id.contribute_tv);
                    TextView name_tv = (TextView) rl.findViewById(R.id.name_tv);
                }else if(position == 2){
                    rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute3, null);
                    TextView contribute_tv = (TextView) rl.findViewById(R.id.contribute_tv);
                    TextView name_tv = (TextView) rl.findViewById(R.id.name_tv);
                }else {
                    rl = (RelativeLayout) View.inflate(context, R.layout.item_contribute, null);
                    TextView contribute_tv = (TextView) rl.findViewById(R.id.contribute_tv);
                    TextView name_tv = (TextView) rl.findViewById(R.id.name_tv);
                    TextView msg_tv = (TextView) rl.findViewById(R.id.no_tv);
                    msg_tv.setText("NO."+(position+1));
                }
                return rl;
            }
        });
        return listView;
    }

}
